export class GameEventBus {


    private listeners: any = {};

    addListener(type: string, listener: (data: any) => void) {
        if (!this.listeners[type]) {
            this.listeners[type] = [];
        }
        this.listeners[type].push(listener);
    }

    removeListener(type: string, listener: (data: any) => void) {
        if (!this.listeners[type]) {
            this.listeners[type] = [];
        }
        const listeners = this.listeners[type];
        const index = listeners.indexOf(listener);
        if (index != -1) {
            listeners.splice(index, 1);
        }
    }

    dispatch(type: string, data: any) {
        const listeners = this.listeners[type];
        if (listeners) {
            for (let listener of listeners) {
                listener(data);
            }
        }
    }
}

export const gameEventBus = new GameEventBus();

window['gameEventBus'] = gameEventBus;